import {
    TAB_KEY_RU_MEN,
    TAB_KEY_TI_SHENG,
    TAB_KEY_QI_TA,
} from "../../configs/index.js";
import {
    getLS,
    formatDate,
} from "../../utils/common.js";

Page({
    data: {
        activeTabKey: TAB_KEY_TI_SHENG,
        tabList: [{
                tabKey: TAB_KEY_RU_MEN,
                tabTitle: "入门阶段",
            },
            {
                tabKey: TAB_KEY_TI_SHENG,
                tabTitle: "提升阶段",
            },
        ],
        articleList: [],
    },
    onLoad: function () {
        var getArticleListByTabKey = () => {
            const englishColze = getLS();
            if (JSON.stringify(englishColze) === "{}") {
                return [];
            }
            const articleList = [];
            const activeArticles = englishColze[this.data.activeTabKey] || {};
            for (let uploadTime in activeArticles) {
                const articleItem = activeArticles[uploadTime];
                if (articleItem.modifyTime) {
                    Object.assign(articleItem, {
                        modifyTimeStr: formatDate(new Date(Number(articleItem.modifyTime)), "yyyy-MM-dd hh:mm:ss"),
                    });
                }
                articleList.push({
                    uploadTime,
                    articleItem,
                });
            }
            if (this.data.activeTabKey === TAB_KEY_QI_TA) {
                articleList.sort((objA, objB) => {
                    return objB.uploadTime - objA.uploadTime;
                });
            } else {
                articleList.sort((objA, objB) => {
                    return objA.uploadTime - objB.uploadTime;
                });
            }
            return articleList;
        }
        let articleList = getArticleListByTabKey();
        this.setData({
            articleList,
        })
    },
    clickArticleItem: function (event) {
        const {
            uploadtime
        } = event.currentTarget.dataset;
        const url = `/pages/fills/fills?activeTabKey=${this.data.activeTabKey}&uploadTime=${uploadtime}`;
        wx.navigateTo({
            url,
            events: {
                updateItem: ({
                    uploadTime,
                    finishCount,
                    modifyTime,
                }) => {
                    const {
                        articleList,
                    } = this.data;
                    for (let i = 0; i < articleList.length; i++) {
                        const item = articleList[i];
                        const {
                            uploadTime: uT,
                            articleItem,
                        } = item;
                        if (uploadTime === uT) {
                            Object.assign(articleItem, {
                                finishCount,
                                modifyTime,
                                modifyTimeStr: formatDate(new Date(Number(modifyTime)), "yyyy-MM-dd hh:mm:ss"),
                            });
                            break;
                        }
                    }
                    this.setData({
                        articleList,
                    });
                },
            },
        })
    }
})
